<template>
  <div class="empDialog">
    <el-dialog title="编辑员工" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手机" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" autocomplete="off" />
        </el-form-item>
        <el-form-item label="入职时间" :label-width="formLabelWidth">
          <!-- 时间选择器 -->

          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期时间"
          />
        </el-form-item>
        <el-form-item label="聘用形式" :label-width="formLabelWidth">
          <el-select
            v-model="form.formOfEmployment"
            placeholder="请选择活动区域"
          >
            <el-option
              v-for="item in hiretype"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" :label-width="formLabelWidth">
          <el-input v-model="form.workNumber" autocomplete="off" />
        </el-form-item>
        <el-form-item label="部门" :label-width="formLabelWidth">
          <el-input
            v-model="form.departmentName"
            autocomplete="off"
            @focus="getTreeData"
          />
          <el-tree
            v-show="isShowTree"
            icon-class=" "
            default-expand-all
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          />
        </el-form-item>
        <el-form-item label="转正时间" :label-width="formLabelWidth">
          <!-- 时间选择器 -->
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { departs } from '@/api/departments'
import { recursiveDepart } from '@/utils/index.js'
import { addEmployees } from '@/api/employees'
import EMPHIRETYPE from '@/constant/employees'
export default {
  name: 'EmpDialog',
  data() {
    return {
      // 表单数据
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 控制dialog弹窗
      dialogFormVisible: false,
      formLabelWidth: '120px',
      // 树的数据
      data: [],
      // 树的展示规则
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 控制树的展示和隐藏
      isShowTree: false,
      // 下拉框的选项
      hiretype: EMPHIRETYPE.hireType
    }
  },
  methods: {
    // 点击树节点时,将部门名称给到form里
    handleNodeClick(data) {
      this.form.departmentName = data.name
      this.isShowTree = false
    },
    // 点击部门,获取数据
    async getTreeData() {
      const {
        data: { depts: res }
      } = await departs()
      const root = res.filter((v) => v.pid == '')
      recursiveDepart(root, res)
      this.data = root
      this.isShowTree = true
    },
    // 点击提交
    async submit() {
      await addEmployees(this.form)
      Object.assign(this.form, {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      })
      // 更新数据
      this.$emit('getEmployees')
      // 退出弹框
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.empDialog {
  ::v-deep .el-form-item__content {
    width: 50%;
  }
  .el-date-editor {
    width: 75%;
  }
  .el-tree {
    width: 100%;
    position: absolute;
    border: 1px solid rgb(220, 223, 230);
    z-index: 998;
  }
}
</style>
